package com.cy.pj.module.controller;

import com.cy.pj.module.pojo.ResponseResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class JsonController {
    /**
     * @ResponseBody
     * 不再将响应结果理解为view
     * 并将响应结果转换为Json(前提是要满足转换条件)
     * 写到响应体
     * (springboot web工程中默认spring web借助jackson框架将pojo或map转换为json格式)
     */
    @RequestMapping("/doConvertResponseToJson")
    @ResponseBody
    public ResponseResult doConvertResponseToJson(){
        ResponseResult result=new ResponseResult();
        result.setCode(200);
        result.setMessage("OK");
        //显示时key使用的是get方法后面的名字首字母小写，没有get方法则不显示该属性
        return result;
    }
    @RequestMapping("/doConverMapToJson")
    @ResponseBody
    public List<Map<String,Object>> doConverMapToJson(){
        List<Map<String,Object>> list=new ArrayList<>();
        Map<String,Object> map1=new HashMap<>();
        Map<String,Object> map2=new HashMap<>();
        map1.put("boolean", false);
        map1.put("内幕", "name");
        map1.put("double", 12.5);
        map1.put(",.", "|");
        list.add(map1);
        map2.put("sadg", false);
        map2.put("说的", "体验");
        map2.put("double", 12.5);
        map2.put("】、", "、");
        list.add(map2);
        return list;
    }
}
